/*
 * test_net_c.cpp
 *
 *  Created on: May 19, 2014
 *      Author: killerg
 */


#include "share/net_c.h"
#include <vector>
#include "share/cs_msg.pb.h"
#include "share/util.h"

using namespace std;
using namespace cs;

#define CONNNUM 1000

static int serial = 0;

bool test_net_c()
{
	vector<tran_c*> trans;
	for (int i=0; i<CONNNUM; ++i) {
		auto tran = new tran_c;
		trans.push_back(tran);
		assert(tran->init());
	}

	while (true) {
		for (auto itr : trans) {
			if (!itr->is_connected() && !itr->is_connecting()) {
				itr->connect(
						"127.0.0.1:9876",
						false,
						[itr, &trans](){
//							printf("client connected %d\n", trans.size());
							auto msg = new OEcho;
							msg->set_str("hello world");
							msg->set_stamp(1);
							itr->send(msg);
						});
			}
			auto msg = itr->recv<OEcho>();
			if (msg) {
				msg->set_stamp(serial);
				itr->send(msg);
				serial ++;
				if (serial % 10000 == 0) {
					printf("%d\n", serial);
				}
			}
			itr->update();
		}
	}

	for (auto itr : trans) {
		itr->destroy();
		delete itr;
	}
	trans.clear();

	return true;
}
